<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- tools栏 -->
      <PageTools>
        <template #left>
          <div class="tips">
            <i class="el-icon-info" />
            <span>本月: 社保在缴 公积金在缴</span>
          </div>
        </template>
        <template #right>
          <el-button type="primary" size="small" @click="$router.push('/import')">导入excel</el-button>
          <el-button type="primary" size="small" @click="handleDownload">导出excel</el-button>
          <el-button type="primary" size="small" @click="visibleDialog = true">新增员工</el-button>
        </template>
      </PageTools>

      <!-- 表格 -->
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" :index="indexMethod" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="110">
            <template #default="scope">
              <!-- <img :src="scope.row.staffPhoto" alt="" width="80"> -->
              <el-image v-if="scope.row.staffPhoto" :src="scope.row.staffPhoto" class="avatar" />
              <div v-else slot="error">
                <img src="@/assets/common/bigUserHeader.png" class="avatar" alt="">
              </div>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formatEmploy(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" width="180">
            <template #default="{row}">
              {{ row.timeOfEntry | timer }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200">
            <template #default="{row}">
              <el-button type="text" size="small" @click="toAvatil(row.id)">查看</el-button>
              <el-button type="text" size="small" @click="showRole(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmploy(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination :total="total" :current-page="params.page" :page-size="params.size" layout="prev, pager, next" @current-change="pageChange" />
        </div>
      </el-card>
    </div>
    <addEmployee :visible-dialog="visibleDialog" @clonewindow="clonewindow" />
    <!-- 分配角色的弹窗 -->
    <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeRole" @open="getRoleList">
      <el-checkbox-group v-model="roleIdList">
        <el-checkbox
          v-for="item in roleList"
          :key="item.id"
          :label="item.id"
        >{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <!-- 这里准备复选框 -->
      <template #footer>
        <el-button type="primary" size="small" @click="giveRole">确定</el-button>
        <el-button size="small" @click="closeRole">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getEmplListApi, delEmplApi } from '@/api/employee'
import { getExportData } from '@/utils/excel'
import addEmployee from './compontens/add-employee.vue'
import { getRoleListAPI } from '@/api/role'
import { AssignRolesEmplApi, getEmplUserApi } from '@/api/employee'
export default {
  // 时间过滤器
  filters: {
    timer(val) {
      return dayjs(val).format('YYYY-MM-DD')
    }
  },
  components: {
    addEmployee
  },
  data() {
    return {
      // 员工列表
      list: [],
      // 请求参数
      params: {
        page: 1,
        size: 5
      },
      // 员工总数
      total: 0,
      // 控制弹窗显示和隐藏
      visibleDialog: false,
      // 控制分配角色的弹窗
      showRoleDialog: false,
      roleIdList: [],
      roleId: null,
      roleList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取员工信息列表
    async getList() {
      const res = await getEmplListApi(this.params)
      this.list = res.rows
      this.total = res.total
    },
    // 序号变成连续的
    indexMethod(index) {
      // return index + 1 + (this.当前页面 - 1) * this.每页显示的条数
      return index + 1 + (this.params.page - 1) * this.params.size
    },
    // 页码变化
    pageChange(page) {
      this.params.page = page
      this.getList()
    },
    // 删除员工
    delEmploy(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        await delEmplApi(id)
        this.$message.success('删除成功!')
        this.getList()
      }).catch(() => { })
    },
    // 处理员工聘用形式
    formatEmploy(value) {
      const EMP = {
        '1': '正式',
        '2': '非正式',
        '4': '其他'
      }
      return EMP[value]
    },
    // 关闭弹窗
    clonewindow() {
      this.visibleDialog = false
      this.getList()
    },
    // 导出表格
    handleDownload() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getEmplListApi(this.params)
        const headerRelation = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        const { data } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header: Object.keys(headerRelation),
          data,
          filename: 'execl-list',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    // 查看个人信息
    toAvatil(id) {
      this.$router.push({
        path: '/avatil',
        query: {
          id
        }
      })
    },
    // 显示添加角色得弹窗
    async showRole(id) {
      this.showRoleDialog = true
      this.roleId = id
      const res = await getEmplUserApi(id)
      this.roleIdList = res.roleIds
    },
    // 关闭添加角色得弹窗
    closeRole() {
      this.showRoleDialog = false
      this.roleIdList = []
      this.roleId = null
    },
    // 打开弹窗获取角色列表信息
    async getRoleList() {
      const res = await getRoleListAPI({
        page: 1, pagesize: 100
      })
      this.roleList = res.rows
    },
    async giveRole() {
      await AssignRolesEmplApi({
        id: this.roleId,
        roleIds: this.roleIdList
      })
      this.$message.success('分配角色成功')
      this.closeRole()
    }
  }
}
</script>

<style lang='scss' scoped>
.avatar {
  width: 50px;
  height: 50px;
  background-color: #f3f3f3;
  object-fit: cover;
  border-radius: 50%;
}
</style>
